#include "jgdatetime.h"

jgdatetime::jgdatetime(/* args */)
{
}

jgdatetime::~jgdatetime()
{
}

jgdatetime::jgdatetime(long timestamp)
{
    m_time = timestamp;
}


jgdatetime jgdatetime::setTimeFormat(string strF)
{
    struct tm stm;
    int iY, iM, iD, iH, iMin, iS;

    char str_time[32] = {0};
    sprintf(str_time,"%s", strF.c_str());

 
    memset(&stm,0,sizeof(stm));
 
    iY = atoi(str_time);
    iM = atoi(str_time+5);
    iD = atoi(str_time+8);
    iH = atoi(str_time+11);
    iMin = atoi(str_time+14);
    iS = atoi(str_time+17);
 
    stm.tm_year=iY-1900;
    stm.tm_mon=iM-1;
    stm.tm_mday=iD;
    stm.tm_hour=iH;
    stm.tm_min=iMin;
    stm.tm_sec=iS;

    long tmpTime = mktime(&stm);

    return jgdatetime(tmpTime);
}

jgdatetime jgdatetime::now()
{
    jgdatetime jd;
    jd.m_time = time(0);
    return jd;
}

long jgdatetime::getTimestamp()
{
    return (long)this->m_time;
}

string jgdatetime::toString(string format)
{
    struct tm *p;
    p = gmtime(&m_time);

    p->tm_hour += 8;
    char s[30] = {0};
    strftime(s, sizeof(s), format.c_str(), p);
    // printf("%d: %s\n", (long)m_time, s);
    return string(s);
}

jgdatetime jgdatetime::addday(int day)
{
    this->m_time += day * 86400;
    return jgdatetime(*this);
}

jgdatetime jgdatetime::addhour(int hour)
{
    this->m_time += hour * 3600;
    return jgdatetime(*this);
}

jgdatetime jgdatetime::addsec(int sec)
{
    this->m_time += sec;
    return jgdatetime(*this);
}

jgdatetime jgdatetime::addmin(int min)
{
    this->m_time += min * 60;
    return jgdatetime(*this);
}
